import java.util.Arrays;

public class Test9 {
    public static void main8(String[] args) {
        int[][] arr = {{1,2,3},{4,5,6}};//arr[行][列]
        int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
        int[][] arr2 = new int[2][3];//0
        //不规则的二维数组
        int[][] arr4 = new int[2][];
        //空指针异常
//        for (int i = 0; i < arr4.length; i++) {
//            for (int j = 0; j < arr4[i].length; j++) {
//                System.out.println(arr4[i][j]);
//            }
//}
        arr4[0] = new int[]{1,2,3};
        arr4[1] = new int[]{1,2,3,4,5,6,7,8};
        System.out.println(Arrays.deepToString(arr4));
        //遍历二维数组
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 3; j++) {
//                System.out.print(arr[i][j] + " ");
//            }
//            System.out.println();
//        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        //打印
        System.out.println(Arrays.deepToString(arr));
        //行的地址
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println("****************");
        //打印每一行
        System.out.println(Arrays.toString(arr[0]));
        System.out.println(Arrays.toString(arr[1]));
        System.out.println("****************");
        //多少列
        System.out.println(arr[0].length);
        System.out.println(arr[1].length);
        //多少行
        System.out.println(arr.length);
    }
    public static void fac4(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){//循环条件
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }
    public static void main7(String[] args) {//数组逆置
        int[] arr1 = {1,2,3,4,5,6,7,8};
        fac4(arr1);
        System.out.println(Arrays.toString(arr1));
    }
    public static boolean fac3(int[] array){//判断是否有3个连续的奇数
        int count = 0;//定义一个计数器
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0){
                count++;//奇数，count++
                if (count == 3){//count=3，true
                    return true;
                }
            }
            else{//遇到偶数计数器置0
                count = 0;
            }
        }return false;
    }
    public static void main6(String[] args) {
        int[] arr = {1,3,4,5,7,9,8,2,1};
        System.out.println(fac3(arr));
    }
    public static int func2(int[] arr1){
        Arrays.sort(arr1);              //排序后的中位数一定是多数元素
        return arr1[arr1.length/2];
    }
    public static void main5(String[] args) {
        //查找多数元素
    int[] arr = {1,2,2,2,2,0,1,2,2};
    int ret = func2(arr);
        System.out.println(ret);
    }

    public static int find(int[] arr){//找到只出现一次的元素
        int ret = arr[0];
        for (int i = 1; i < arr.length; i++) {
            //0^n=n n^n=0
            ret ^= arr[i];
        }return ret;
    }
    public static void main3(String[] args) {
            int[] array = {1,2,3,2,1};
            int ret = find(array);
        System.out.println(ret);
    }
    public static int[] fac2(int[] arr,int t){
        //遍历数组
        for (int i = 0; i < arr.length - 1; i++) {
            //i从0开始判断
            for (int j = i+1; j < arr.length; j++) {
                //j从1开始判断
                //判断
                if(arr[i] + arr [j] == t){
                    return new int[]{i,j};
                }
            }
        }return new int[]{-1,-1};
    }
    public static void main2(String[] args) {//求两个数相加的目标值，并返回其数组下标
        int[] array = {2,1,9,4};
        int[] ret = fac2(array, 6);
        System.out.println(Arrays.toString(ret));
    }
    public static void fac1(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            //使用左右指针对数组进行检查
            while (left < right && arr[left] % 2 != 0){
                //若左指针遍历的是奇数，则左指针++
                left++;
            }
            while (left < right && arr[right] % 2 == 0){
                //若左指针遍历的是偶数，则右指针--
                right--;
            }
            //如果不进入循环则交换位置
            int tmp = 0;
            tmp = arr[right];
            arr[right] = arr[left];
            arr[left] = tmp;
        }
    }
    public static void main1(String[] args) {
        int[] array = {8,2,4,6,10};
        System.out.println(Arrays.toString(array));
        fac1(array);
        System.out.println(Arrays.toString(array));
    }
}
